package com.dykj.fireFighting.web.controller.GB26875Controller;

import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.web.datasource.RequestDataHelper;
import com.dykj.fireFighting.web.enums.FlagDelEnum;
import com.dykj.fireFighting.web.model.GB26875;
import com.dykj.fireFighting.web.model.Manufactor;
import com.dykj.fireFighting.web.model.report.DevHis;
import com.dykj.fireFighting.web.service.DevHisService;
import com.dykj.fireFighting.web.service.ManufactorService;
import com.dykj.fireFighting.web.utils.ReturnUtil;
import com.dykj.fireFighting.web.utils.UUIDUtils;
import io.swagger.annotations.ApiOperation;
import org.quickserver.net.AppException;
import org.quickserver.net.GB268753QN.sendClient;
import org.quickserver.net.model.DevType;
import org.quickserver.net.model.GBServerDataAll;
import org.quickserver.net.server.QuickServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/test")
public class TestQN {
    @Autowired
    private ManufactorService manufactorService;
    @Autowired
    private DevHisService devHisService;

    @GetMapping("/openPortQN")
    @ResponseBody
    public ModelMap openPort() {
        QuickServer myServer =
                new QuickServer("com.dykj.aegir.controller.GB26875.GB26875Controller");
        myServer.setPort(9288);
        myServer.setName("EchoServer v 1.0");
        myServer.setTimeout(300000);
        myServer.setClientBinaryHandler("com.dykj.aegir.controller.GB26875.GB26875Controller");
        //myServer.setClientData("GBServerData");
        Map<String, Object> map = new HashMap<>();
        map.put("flagDel", FlagDelEnum.notDel.getCode());
        List<Manufactor> list = manufactorService.queryList(map);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getTemplate().equals("1")) {
                GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0101"); //青鸟
            } else if (list.get(i).getTemplate().equals("2")) {
                GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0103"); //利达
            } else if (list.get(i).getTemplate().equals("3")) {
                GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0102"); //海湾
            } else if (list.get(i).getTemplate().equals("4")) {
                GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0104"); //水设备
            }
        }

        try {
            myServer.startServer();
        } catch (AppException e) {
            System.err.println("Error in server : " + e);
        }
        return ReturnUtil.Success("操作成功");
    }

    @GetMapping("/sendQN")
    @ResponseBody
    public Map<String, Object> send(@RequestBody GB26875 gb26875) throws IOException, InterruptedException, ParseException {
        // DevHisService  devHisService= BeanContext.getBean(DevHisService.class);
        //System.out.println(lfh.hi());
        /*Map<String, Object> map = new sendClient().QNSend89("00000002000180001100024000100001");
        return map;*/

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String,Object> map=null;
        switch (gb26875.getType()){
            case "1":
                if("".equals(gb26875.getDate())){
                    map=new sendClient().QNSend189(gb26875.getUniqueCode(),null);//设置用户信息传输装置报名时间    1
                }else{
                    map=new sendClient().QNSend189(gb26875.getUniqueCode(),sdf.parse(gb26875.getDate()));//设置用户信息传输装置报名时间
                }
                break;
            case "2":
                map=new sendClient().QNSend188(gb26875.getUniqueCode());//读用户信息传输装置生产日期    1
                break;
            case "3":
                map=new sendClient().QNSend91(gb26875.getUniqueCode(),Integer.parseInt(gb26875.getMin()));//查岗命令   1
                break;
            case "4":
                if("".equals(gb26875.getDate())){
                    map=new sendClient().QNSend90(gb26875.getUniqueCode(),null);//同步用户信息传输装置时间   1
                }else{
                    map=new sendClient().QNSend90(gb26875.getUniqueCode(),sdf.parse(gb26875.getDate()));//同步用户信息传输装置时间
                }
                break;
            case "5":
                map=new sendClient().QNSend89(gb26875.getUniqueCode());//初始化用户信息传输装置    1
                break;
            case "6":
                map=new sendClient().QNSend88(gb26875.getUniqueCode());//读用户信息传输装置系统时间   1
                break;
            case "7":
                map=new sendClient().QNSend86(gb26875.getUniqueCode());//读用户信息传输装置配置情况    1
                break;
            case "8":
                map=new sendClient().QNSend85(gb26875.getUniqueCode());//读用户信息传输装置软件版本   1
                break;
            case "9":
                map=new sendClient().QNSend84(gb26875.getUniqueCode(),sdf.parse(gb26875.getDate()),Integer.parseInt(gb26875.getTotal()));//读用户信息传输装置操作信息记录     1
                break;
            case "10":
                map=new sendClient().QNSend81(gb26875.getUniqueCode());//读用户信息传输装置运行状态   1
                break;
            case "11":
                map=new sendClient().QNSend68(gb26875.getUniqueCode(),Integer.parseInt(gb26875.getSysType()),Integer.parseInt(gb26875.getSysAddress()));//读建筑消防设施系统时间   1
                break;
            case "12":
                map=new sendClient().QNSend67(gb26875.getUniqueCode(), gb26875.getList());//读建筑消防设施部件配置情况    1
                break;
            case "13":
                map=new sendClient().QNSend66(gb26875.getUniqueCode(),gb26875.getList());//读建筑消防设施系统配置情况
                break;
            case "14":
                map=new sendClient().QNSend65(gb26875.getUniqueCode(),Integer.parseInt(gb26875.getSysType()),Integer.parseInt(gb26875.getSysAddress()));//读建筑消防设施软件版本    1
                break;
            case "15":
                map=new sendClient().QNSend64(gb26875.getUniqueCode(),sdf.parse(gb26875.getDate()),Integer.parseInt(gb26875.getTotal()),Integer.parseInt(gb26875.getSysType()),Integer.parseInt(gb26875.getSysAddress()));//读建筑消防设施操作信息记录
                break;
            case "16":
                map=new sendClient().QNSend63(gb26875.getUniqueCode(),gb26875.getList());//读建筑消防设施系统部件模拟量值
                break;
            case "17":
                map=new sendClient().QNSend62(gb26875.getUniqueCode(),gb26875.getList());//读建筑消防设施系统部件状态    1
                break;
            case "18":
                map=new sendClient().QNSend61(gb26875.getUniqueCode(),gb26875.getList());//读建筑消防设施系统状态
                break;
        }
        return map;
    }

    @ApiOperation(value = "分表测试-插入数据")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    @ResponseBody
    public void create(String id) {
        DevHis devHis = new DevHis();
        devHis.setId(UUIDUtils.get32UUID()); //code
        devHis.setDevCode(UUIDUtils.get32UUID()); //传输装置唯一编码
        devHis.setLx("1"); //类型  1传输装置   2设备
        devHis.setBjzt(DevType.cszglx.getCode()); //部件状态码
        devHis.setBjztSm(DevType.cszglx.getName()); //部件状态中文说明
        devHis.setStatus("0"); //复核状态 默认未复核
        devHis.setRelationId(UUIDUtils.get32UUID()); //id
        int count1 = devHisService.insertHis(devHis);
        System.out.println(count1);
    }

    @ApiOperation(value = "分表测试-查询数据")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult select(String id) {
        Map<String, Object> patams = new HashMap();
        patams.put("id", id);
        RequestDataHelper.setRequestData(patams);
        DevHis devHis = devHisService.getNewestGz("d9bbb48217ba41f1b5dc5ff280fa9166");
        return CommonResult.success(devHis);
    }
}
